import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o phoneList = [];
    @o typeList = [];
    @o modelId = '';

    will() {

    }

    closeDialog() {
        this.visible = false;
        this.phoneList = [];
        this.typeList = [];
        this.modelId = '';
    }

    addCustomPhone() {
        this.phoneList.push({
            phone: '',
            countryCode: '',
            selectCountry: this.stores.Frame.defaultCode
        });
    }

    phoneChange(v, item) {
        item.phone = v;
    }

    selectCodeItem(selectItem, item) {
        if (selectItem.code === item.selectCountry.code) {
            return;
        }
        item.selectCountry = selectItem;
        item.countryCode = selectItem.code === $$.SEM.CN_COUNTRY_CODE ? '' : selectItem.code;
        item.phone = '';
    }

    deleteItem(index) {
        this.phoneList.splice(index, 1);
    }

    typeSelect(type) {
        if (!!~this.typeList.indexOf(type)) {
            this.typeList = this.typeList.filter(item => {
                return item !== type;
            });
            if (type === 1) {
                this.phoneList = [];
            }
        } else {
            this.typeList.push(type);
            if (type === 1) {
                this.phoneList = [];
                this.addCustomPhone();
            }
        }
    }

    getModelInfo() {
        this.stores.Frame.showLoading();
        request({
            url: $$.api('modelsInfo'),
            data: {id: this.modelId}
        }).then(res => {
            this.stores.Frame.hideLoading();
            this.typeList = res.data.modelStyle;
            if (res.data.userList && res.data.userList.length > 0) {
                res.data.userList.map(item => {
                    let code = item.countryCode || $$.SEM.CN_COUNTRY_CODE;
                    let selectCountry = this.stores.Frame.countryCodeList.find(i => {
                        return code === i.code;
                    })
                    this.phoneList.push({
                        phone: item.phone,
                        countryCode: item.countryCode || '',
                        selectCountry: selectCountry
                    });
                });
            } else {
                this.phoneList = [];
                this.addCustomPhone();
            }
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        })
    }

    confirmDialog() {
        let phoneList = this.phoneList.filter(item => {
            return !!item.phone;
        });
        if (phoneList.length === 0 && !!~this.typeList.indexOf(1)) {
            this.stores.Message.showMessage(3, '请至少输入一个需要配置的用户手机号');
            return;
        }
        this.stores.Frame.showLoading();
        request({
            url: $$.api('modelsConfig'),
            method: 'post',
            data: {
                id: this.modelId,
                modelType: this.typeList,
                phoneList: phoneList
            }
        }).then(res => {
            this.stores.Frame.hideLoading();
            this.closeDialog();
            this.stores.Message.showMessage(1, res.message);
            this.fire('modelsConfig:update');
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });

    }


    mount() {
        this.on('modelsConfig:show', (data) => {
            if (data) {
                this.visible = true;
                this.modelId = data.id;
                this.getModelInfo();
            }
        });
    }

}

export default Store;